﻿using System;
using System.Collections.Generic;
using System.Text;
using daBase.Analyser;
using daSyntaxAnalyser.SyntaxTree;
using daStaticAnalyser.Analysers;
using daStaticAnalyser;

namespace DifferentAnalizers
{
    [AnalyserDefaultSettings("Константное выражение.", "Значение выражения всегда константа!", 50, "Необходимо заменить выражение константой.")]
    public class ConstExpressionAnalyser : BaseStaticAnalyser
    {
        private bool IsConstant(ExpressionElement Expression)
        {
            if (Expression is ConstExpressionElement)
                return true;
            if (Expression is UnaryExpressionElement)
                return IsConstant((Expression as UnaryExpressionElement).Operand);
            if (Expression is BinaryExpressionElement)
                return (IsConstant((Expression as BinaryExpressionElement).Left) && IsConstant((Expression as BinaryExpressionElement).Right));
            return false;
        }

        /// <summary>
        /// Собственно анализ
        /// </summary>
        /// <param name="e">параметр анализа</param>
        protected override void DoAnalyse(SyntaxTreeArgs e)
        {
            if ((e.Element is ExpressionElement) && !(e.Element is ConstExpressionElement) && IsConstant(e.Element as ExpressionElement))
                AddError(e.Element.StartInfo, e.Element.ToString());
        }
    }
}
